<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use App\Components\Tools;
use App\Components\DyToools;
use App\Models\DyModels\Douyin;
use App\Models\DylistModels\DyTopic;
use App\Models\DylistModels\DyBangdanVideo;
use App\Models\DylistModels\DyHotVideo;
use App\Models\DylistModels\DySport;
use App\Models\DylistModels\DyAmusement;
use App\Models\DylistModels\DyGame;
use App\Models\DylistModels\DyFood;
use App\Models\DylistModels\DyDrama;
use App\Models\DylistModels\DyCar;
use App\Models\DylistModels\DyTravel;
use App\Models\DylistModels\DyCospa;
use App\Models\DylistModels\DyStars;
use App\Models\DylistModels\DyHotsearchVideo;
use App\Models\DylistModels\DyHotsearchTrending;
use App\Models\DylistModels\DyHotsearchWord;
use App\Models\DyModels\DyuserQyhVideo;
use App\Models\DyModels\DyuserQyhFans;

/**
 * 获取抖音热门素材
 */
class DyPopularMaterial extends Command {

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'DyPopularMaterial';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取抖音热门素材';

    private $url = '';    
    
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() {
        Tools::commandsLog('DyPopularMaterial:' . date("Y-m-d H:i:s"));
        $this->url = Tools::getOneconfig("/config/third.json", array('douyin', 'dy_url'));
        $this->getPopularMaterial();
    }

    //生成cltoken
    function createcltoken() {
        $DyToools = new DyToools();
        $clienttk = $DyToools->client_token();
        return $clienttk;
    }

    //热门素材总方法-------------------------------------------------------------------------------------------
    public function getPopularMaterial() {
        $data = $this->createcltoken();
        $access_token = $data['data']['access_token'];
        //  print_r($access_token);exit();
        if (!empty($access_token)) {
            $this->gethotsearch($access_token);          //获取抖音实时热点词
            $this->gettrendingsearch($access_token);    //获取抖音上升词
            $this->gethotSearchVideo($access_token);    //获取热度词聚合视频
            $this->gettopic($access_token);             //获取抖音话题数据 并入库（入库抖音话题榜）
            $this->gethotword($access_token);      //获取抖音热门视频内容
            $this->getStars($access_token);  //获取抖音娱乐明星数据
            $this->getDySport($access_token);  //抖音体育榜单
            $this->getAmusement($access_token);    //抖音搞笑榜单
            $this->getGame($access_token);    //抖音游戏资讯榜单
            $this->getFood($access_token);    //抖音美食资讯榜单
            $this->getDrama($access_token);    //抖音剧情资讯榜单
            $this->getCar($access_token);    //抖音汽车资讯榜单
            $this->getTravel($access_token);    //抖音旅游资讯榜单
            $this->getCospa($access_token);    //抖音二次元资讯榜单
        }
    }

    //获取抖音实时热点词
    function gethotsearch($access_token) {
        $DyHotsearchWord = new DyHotsearchWord();
        $url = $this->url . "/hotsearch/sentences/?access_token=$access_token";
        $data = $this->getJson($url);
        if ($data['extra']['error_code'] == 0) {
            return $DyHotsearchWord->upHotsearchWord($data['data']['list']);
        }
        return $data;
    }

    //获取抖音上升词
    function gettrendingsearch($access_token) {
        $upDyHotsearchTrending = new DyHotsearchTrending();

        $url = $this->url . "/hotsearch/trending/sentences/?access_token=$access_token&cursor=0&count=20";
        $data = $this->getJson($url);
        if ($data['extra']['error_code'] == 0) {
            return $upDyHotsearchTrending->upDyHotsearchTrending($data['data']['list']);
        }
        return $data;
    }

    //获取热度词聚合视频
    function gethotSearchVideo($access_token) {
        $DyHotsearchWord = new DyHotsearchWord();
        $date = $DyHotsearchWord->getword();
        if (is_array($date)) {
            foreach ($date as $k => $v) {
                $hot_sentence = $v['sentence'];
                $wid = $v['id'];
                $url = $this->url . "/hotsearch/videos/?access_token=$access_token&hot_sentence=" . urlencode($hot_sentence);
                $data = $this->getJson($url);
                //   echo '<pre/>';     print_r($data);
                $info = $data['data']['list'];

                if ($data['extra']['error_code'] == 0) {
                    foreach ($info as $kk => $vv) {
                        $data = array(
                            'wid' => $wid,
                            'share_url' => $vv['share_url'],
                            'title' => $vv['title'],
                            'cover' => $vv['cover'],
                            'item_id' => $vv['item_id'],
                            'video_status' => $vv['video_status'],
                            'comment_count' => $vv['statistics']['comment_count'],
                            'digg_count' => $vv['statistics']['digg_count'],
                            'download_count' => $vv['statistics']['download_count'],
                            'forward_count' => $vv['statistics']['forward_count'],
                            'play_count' => $vv['statistics']['play_count'],
                            'share_count' => $vv['statistics']['share_count'],
                            'create_time' => date('Y-m-d H:i:s', time()),
                            'update_time' => date('Y-m-d H:i:s', time()),
                        );
                        // echo '<pre/>';  print_r($data);exit();
                        //  $date=json_encode($data);
                        $DyHotsearchVideo = new DyHotsearchVideo();
                        $DyHotsearchVideo->upadinfo($data);
                    }
                }
            }
        }
    }

    //获取抖音话题数据 并入库（入库抖音话题榜）
    function gettopic($access_token) {
        $accessToken = new DyTopic();
        $url = $this->url . "/data/extern/billboard/topic/?access_token=$access_token";
        $data = $this->getJson($url);
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upTopicto($data['data']['list']);
        }
    }

    //获取抖音娱乐明星数据
    function getStars($access_token) {
        $accessToken = new DyStars();
        $url = $this->url . "/data/extern/billboard/stars/?access_token=$access_token";
        $data = $this->getJson($url);
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upStarsto($data['data']['list']);
        }
    }

    //抖音体育榜单
    function getDySport($access_token) {
        $accessToken = new DySport();
        $url = $this->url . "/data/extern/billboard/sport/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upDySportto($date);
        }
    }

    //获取抖音热门视频内容
    function gethotword($access_token) {
        $accessToken = new DyHotVideo();
        $url = $this->url . "/data/extern/billboard/hot_video/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upHotVideoto($date);
        }
        return false;
    }

    //抖音搞笑榜单
    function getAmusement($access_token) {
        $accessToken = new DyAmusement();
        $url = $this->url . "/data/extern/billboard/amusement/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upAmusementto($date);
        }
        return false;
    }

    //抖音游戏资讯榜单
    function getGame($access_token) {
        $accessToken = new DyGame();
        $url = $this->url . "/data/extern/billboard/game/inf/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upGameto($date);
        }
        return false;
    }

    //抖音美食榜榜单
    function getFood($access_token) {
        $accessToken = new DyFood();
        $url = $this->url . "/data/extern/billboard/food/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upFoodto($date);
        }
        return false;
    }

    //抖音剧情榜榜单
    function getDrama($access_token) {
        $accessToken = new DyDrama();
        $url = $this->url . "/data/extern/billboard/drama/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upDramato($date);
        }
        return false;
    }

    //抖音汽车榜榜单
    function getCar($access_token) {
        $accessToken = new DyCar();
        $url = $this->url . "/data/extern/billboard/car/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upCarto($date);
        }
        return false;
    }

    //抖音旅游榜榜单
    function getTravel($access_token) {
        $accessToken = new DyTravel();
        $url = $this->url . "/data/extern/billboard/travel/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upTravelto($date);
        }
        return false;
    }

    //抖音二次元榜榜单
    function getCospa($access_token) {
        $accessToken = new DyCospa();
        $url = $this->url . "/data/extern/billboard/cospa/overall/?access_token=$access_token";
        $data = $this->getJson($url);
        $date = $data['data']['list'];
        if ($data['extra']['error_code'] == 0) {
            return $accessToken->upCospato($date);
        }
        return false;
    }
    
    //get请求
    function getJson($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//为true，则会跟踪爬取重定向页面，否则，不会跟踪重定向页面
        curl_setopt($ch, CURLOPT_ENCODING, "");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }    
    
}
